package com.llb666.java.collection;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 线性安全的HashMap:
 *  Collections.synchronizedMap(Map<K,V> m)
 *  ConcurrentHashMap
 * @author liulibin
 */
public class SafeHashMapDemo {
    public static void main(String[] args) {
        Map<String,String> hashMap = new HashMap<>();
        Map<String,String> safeHashMap = Collections.synchronizedMap(hashMap);
        safeHashMap.put("aa","1");
        safeHashMap.put("bb","2");
        System.out.println(safeHashMap.get("bb"));

        // -----------------------------------------------
        Map<String,String> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put("cc","3");
        concurrentHashMap.put("dd","4");
        System.out.println(concurrentHashMap.get("cc"));
    }
}
